/****************************************************************************
 * Copyright (C) 2009-2011 Dimok
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "SettingsMenu.h"
#include "GUISettingsMenu.hpp"
#include "AudioSettingsMenu.hpp"
#include "CustomPathsSM.hpp"
#include "ThemeSettingsMenu.hpp"
#include "ControlsSettingsMenu.hpp"
#include "ParentalControlSM.hpp"
#include "FeaturesSettingsMenu.hpp"
#include "UpdateSettingsMenu.hpp"
#include "../../Prompts/CreditsWindow.h"
#include "../../Prompts/PromptWindows.h"
#include "../../Controls/Application.h"
#include "../../Miis/MiiInstaller.h"

SettingsMenu::SettingsMenu()
	:GuiFrame(screenwidth, screenheight)
 {
	fixEMiiNeeded = false;
	settingsBrowser = NULL;
	
	BtnSoundClick = Resources::GetSound("button_click.wav");
	BtnSoundOver = Resources::GetSound("button_over.wav");
	BtnSoundClick->SetVolume(Settings.SFXVolume);
	BtnSoundOver->SetVolume(Settings.SFXVolume);
	
	trigA.SetSimpleTrigger(-1, WiiControls.ClickButton | ClassicControls.ClickButton << 16, GCControls.ClickButton);
	
	BgImgData = Resources::GetImageData("settings_background.png");
	BgImg = new GuiImage(BgImgData);
	
	Title = new GuiText(tr("Global Settings"), thInt("28 - settings menu title text size"), thColor("r=0 g=0 b=0 a=255 - settings menu title text color"));
	Title->SetAlignment(thAlign("left - settings menu title text align hor") | thAlign("top - settings menu title text align ver"));
	Title->SetPosition(thInt("170 - settings menu title text pos x"), thInt("38 - settings menu title text pos y"));
	Title->SetMaxWidth(thInt("310 - settings menu title text maxwidth"), SCROLL_HORIZONTAL);
	
	BackBtnTxt = new GuiText(tr( "Back" ), thInt("24 - settings menu back button text size"), thColor("r=0 g=0 b=0 a=255 - settings menu back button text color"));
	BackBtn = new PictureButton("settings_menu_button.png", "settings_menu_button.png", BtnSoundClick, BtnSoundOver);
	BackBtn->SetAlignment(thAlign("center - settings menu back button align hor") | thAlign("top - settings menu back button align ver"));
	BackBtn->SetPosition(thInt("-195 - settings menu back button pos x"), thInt("400 - settings menu back button pos y"));
	BackBtn->SetLabel(BackBtnTxt);
	BackBtn->SetTrigger(&trigA);
	BackBtn->Clicked.connect(this, &SettingsMenu::OnBackButtonClick);
	
	globalSettings = new GlobalSettings();
	globalSettings->FlyingBtnMenuClicked.connect(this, &SettingsMenu::OnFlyingButtonClick);
	
	Append(BgImg);
	Append(Title);
	Append(BackBtn);
	Append(globalSettings);
	
	if (Application::Instance()->isReloadingTheme())
	{
		Title->SetText(tr( "Theme Settings" ));
		settingsBrowser = new ThemeSettingsMenu();
		settingsBrowser->ThemeReloadRequest.connect(this, &SettingsMenu::OnThemeReloadRequest);
		Append(settingsBrowser);
	}
	else if(Application::Instance()->isReloadingFont())
	{
		Title->SetText(tr( "Custom Paths" ));
		settingsBrowser = new CustomPathsSM();
		settingsBrowser->ThemeReloadRequest.connect(this, &SettingsMenu::OnThemeReloadRequest);
		Append(settingsBrowser);
	}
	else if(Application::Instance()->isReloadingLang())
	{
		Title->SetText(tr( "GUI Settings" ));
		settingsBrowser = new GuiSettingsMenu();
		settingsBrowser->ThemeReloadRequest.connect(this, &SettingsMenu::OnThemeReloadRequest);
		Append(settingsBrowser);
	}
	else
	{
		SetEffect(EFFECT_FADE, 20);
		
		globalSettings->FadeIn();
	}
}

SettingsMenu::~SettingsMenu()
{
	RemoveAll();
	
	if(globalSettings)
		delete globalSettings;
	
	delete Title;
	
	delete BgImg;
	Resources::Remove(BgImgData);
	
	delete BackBtnTxt;
	delete BackBtn;
	
	Resources::Remove(BtnSoundClick);
	Resources::Remove(BtnSoundOver);
}

void SettingsMenu::SetTitle(std::string title)
{
	if(Title)
		Title->SetText(title);
}

void SettingsMenu::OnBackButtonClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	Application::Instance()->ResetPads();
	
	if(!settingsBrowser)
	{
		if(globalSettings)
			globalSettings->hide();
		
		Settings.Save();
		
		SetEffect(EFFECT_FADE, -20);
		Close();
	}
	else
	{
		settingsBrowser->Close();
		Application::Instance()->updateEvents();
		settingsBrowser = NULL;
		
		SetTitle(tr("Global Settings"));
		globalSettings->FadeIn();
		
		if(fixEMiiNeeded)
		{
			fixEMiiNeeded = false;
			Mii::Fix_EmuDB();
		}
	}
}

void SettingsMenu::OnFlyingButtonClick(int menu)
{
	if(menu == SettingsMenu::DEFAULT)
	{
		if (Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_RESET_SETTINGS))
		{
			if(WindowPrompt(tr( "Are you sure you want to reset?" ), 0, tr( "Yes" ), tr( "Cancel" )))
			{
				Application::Instance()->setReload(Application::RELOAD_DEFAULT);
				
				Settings.SetDefault();
				Theme::SetDefault();
				
				Settings.Save();
				Settings.LoadLanguage(Settings.LanguageFile);
				
				ForcedClosing(this);
			}
		}
		else
			WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
			
	}
	else if(menu == SettingsMenu::CREDITS)
	{
		CreditsWindow * creditsWindow = new CreditsWindow();
		Append(creditsWindow);
	}
	else
	{
		globalSettings->hide();
		
		if(menu == SettingsMenu::GUI)
		{
			if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_GUI_SETTINGS))
			{
				SetTitle(tr( "GUI Settings" ));
				settingsBrowser = new GuiSettingsMenu();
				settingsBrowser->ThemeReloadRequest.connect(this, &SettingsMenu::OnThemeReloadRequest);
			}
			else
			{
				WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
				goto abort;
			}
		}
		else if(menu == SettingsMenu::AUDIO)
		{
			if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_AUDIO_SETTINGS))
			{
				SetTitle(tr( "Audio Settings" ));
				settingsBrowser = new AudioSettingsMenu();
			}
			else
			{
				WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
				goto abort;
			}
		}
		else if(menu == SettingsMenu::PATH)
		{
			if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_CUSTOMPATH_SETTINGS))
			{
				SetTitle(tr( "Custom Paths" ));
				settingsBrowser = new CustomPathsSM();
				settingsBrowser->ThemeReloadRequest.connect(this, &SettingsMenu::OnThemeReloadRequest);
				settingsBrowser->FixEMiiDB.connect(this, &SettingsMenu::OnFixEMiiDB);
			}
			else
			{
				WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
				goto abort;
			}
		}
		else if(menu == SettingsMenu::THEME)
		{
			if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_THEME_SETTINGS))
			{
				SetTitle(tr( "Theme Settings" ));
				settingsBrowser = new ThemeSettingsMenu();
				settingsBrowser->ThemeReloadRequest.connect(this, &SettingsMenu::OnThemeReloadRequest);
			}
			else
			{
				WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
				goto abort;
			}
		}
		else if(menu == SettingsMenu::CONTROL)
		{
			if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_CONTROLS_SETTINGS))
			{
				SetTitle(tr( "Controller Settings" ));
				settingsBrowser = new ControlsSettingsMenu();
			}
			else
			{
				WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
				goto abort;
			}
		}
		else if(menu == SettingsMenu::PARENTAL)
		{
			if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_PARENTAL_SETTINGS))
			{
				SetTitle(tr( "Parental Control" ));
				settingsBrowser = new ParentalControlSM();
			}
			else
			{
				WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
				goto abort;
			}
		}
		else if(menu == SettingsMenu::FEATURES)
		{
			if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_FEATURES_MENU))
			{
				SetTitle(tr( "Features Menu" ));
				settingsBrowser = new FeaturesSettingsMenu();
				settingsBrowser->FixEMiiDB.connect(this, &SettingsMenu::OnFixEMiiDB);
			}
			else
			{
				WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
				goto abort;
			}
		}
		else if(menu == SettingsMenu::UPDATE)
		{
			if(Settings.GodMode || !(Settings.ParentalBlocks & BLOCK_UPDATES))
			{
				SetTitle(tr( "Update Settings" ));
				settingsBrowser = new UpdateSettingsMenu();
			}
			else
			{
				WindowPrompt(tr( "Permission denied." ), tr( "Console must be unlocked for this option." ), tr( "OK" ));
				goto abort;
			}
		}
		
		Append(settingsBrowser);
		return;
		
	  abort:
		globalSettings->FadeIn();
	}
}

void SettingsMenu::OnFixEMiiDB()
{
	fixEMiiNeeded = true;
}

void SettingsMenu::OnThemeReloadRequest()
{
	Remove(settingsBrowser);
	delete settingsBrowser;
	settingsBrowser = NULL;
	
	ForcedClosing(this);
}
